//
//  SettingsViewController.m
//  WeightLog
//
//  Created by Boky on 4/28/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SettingsViewController.h"

@implementation SettingsViewController


@synthesize saveButton, table, nameTableViewCell, emailTableViewCell, defaultRecipientEmailTableViewCell, weightUnitTableViewCell, defaultEmailRecipientLabel;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	self.defaultEmailRecipientLabel = nil;
	self.saveButton = nil;
	self.table = nil;
	self.nameTableViewCell = nil;
	self.emailTableViewCell = nil;
	self.defaultRecipientEmailTableViewCell = nil;
	self.weightUnitTableViewCell = nil;
}



- (IBAction)saveAction:(id)sender{
	
	// Save application settings
	[self.parentViewController dismissModalViewControllerAnimated:YES];

}

- (void)dealloc {
	[defaultEmailRecipientLabel release];
	[nameTableViewCell release];
	[emailTableViewCell release];
	[defaultRecipientEmailTableViewCell release];
	[weightUnitTableViewCell release];
	[table release];
	[saveButton release];
    
	[super dealloc];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	// First, see the section
	if(indexPath.section == 0){
		if(indexPath.row == 0)
			return nameTableViewCell;
		else if(indexPath.row ==1)
			return emailTableViewCell;
		else if(indexPath.row ==2)
			return defaultRecipientEmailTableViewCell;
	}
	
	return weightUnitTableViewCell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;	
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

	if(section == 0)
		return 3;
	else return 1;
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if(indexPath.row == 0)
		NSLog(@"click row 0");
	else if(indexPath.row == 1)
		NSLog(@"click row 1");
	else if(indexPath.row == 2)
		[self showPeoplePickerController];
	else if(indexPath.row == 3)
		NSLog(@"click row 3");
		
	NSLog(@"click imposible!!!");
	
	
}

#pragma mark Show all contacts
// Called when users tap "Display Picker" in the application. Displays a list of contacts and allows users to select a contact from that list.
// The application only shows the phone, email, and birthdate information of the selected contact.
-(void)showPeoplePickerController {
	
	ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
	
	// Display only a person's phone, email, and birthdate
	NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt: kABPersonPhoneProperty], 
							   [NSNumber numberWithInt: kABPersonEmailProperty],
							   [NSNumber numberWithInt: kABPersonBirthdayProperty], nil];
	
	
	picker.displayedProperties = displayedItems;
	// Show the picker 
	[self presentModalViewController:picker animated:YES];
    [picker release];	
}

#pragma mark ABPeoplePickerNavigationControllerDelegate methods
// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    
	return YES;
}


// Does not allow users to perform default actions such as dialing a phone number, when they select a person property.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
								property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
	

	ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
	CFStringRef email = ABMultiValueCopyValueAtIndex(emails, identifier);
	self.defaultEmailRecipientLabel.text =  (NSString *) email;
	[[peoplePicker parentViewController] dismissModalViewControllerAnimated:YES];
	return NO;
}


// Dismisses the people picker and shows the application when users tap Cancel. 
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
	[self dismissModalViewControllerAnimated:YES];
}



@end
